<?php
class Admin_Services extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url','authentication_helper'));
		$this->load->library('session');
		$this->load->model(array('services_model','service_items_model'));
	}
	
	function index(){
		doNeedLogin(uri_string());
		
		$rs = $this->services_model->get_services();
		$data['services'] = $rs->result_array();
		//$data['fields'] = $rs->list_fields();
		$data['fields'] = $this->db->list_fields('services');
		$data['page_title'] = 'Services - Administration';
		$data['menu_item'] = 'Services';
		$data['user'] = $this->session->userdata('is_logged');
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/services', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	function edit(){
		doNeedLogin(uri_string());
		$id = $this->input->post("ID");
		$description = $this->input->post("description");
		$img = $this->input->post("img");
		$data = array(
               'description' => $description,
			   'image_url' => $img,
        ); 
		$this->db->update('services', $data, array('id' => $id));
		if ($this->db->affected_rows() > 0) {
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('success'=>false,"reason"=>"query update not sucessful"));
		} 
	}
	
	function load_service_item($type){
		doNeedLogin(uri_string());
		
		$rs = $this->service_items_model->get_items($type);
		$data['items'] = $rs->result_array();
		//$data['fields'] = $rs->list_fields();
		$data['fields'] = $this->db->list_fields('service_items');
		$data['type'] = $type;
		if ($type == 'webvideo')
		{
			$type = 'Web Video';
		} else if ($type == 'trainingvideo')
		{
			$type = 'Training Video';
		} else if ($type == 'corporatevideo')
		{
			$type = 'Corporate Video';
		}
		$data['page_title'] = $type.' - Administration';
		$data['menu_item'] = $type;
		$data['user'] = $this->session->userdata('is_logged');
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/service_item', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	function addItem($type){
		doNeedLogin(uri_string());
		$title = $this->input->post("title");
		$description = $this->input->post("description");
		$img = $this->input->post("newImg");
		$data = array(
		  'title' => $title,
		  'description' => $description,
		  'image_url' => $img,
		  'type' => $type,
		); 
		$this->db->insert('service_items', $data); 
		if ($this->db->affected_rows() == 1) {
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('success'=>false,"reason"=>"query update not sucessful"));
		}
	}
	
	function editItem(){
		doNeedLogin(uri_string());
		$id = $this->input->post("ID");
		$title = $this->input->post("title");
		$description = $this->input->post("description");
		$img = $this->input->post("img");
		$data = array(
				'title' => $title,
			   'description' => $description,
			   'image_url' => $img,
		); 
		$this->db->update('service_items', $data, array('id' => $id));
		if ($this->db->affected_rows() > 0) {
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('success'=>false,"reason"=>"query update not sucessful"));
		}
	}
	
	function deleteItem(){
		doNeedLogin(uri_string());
		$ids = $this->input->post("ids");
		$this->db->where_in('id', $ids );
		$this->db->delete('service_items');
		if ($this->db->affected_rows() > 0) {
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('success'=>false,"reason"=>"query update not sucessful"));
		}
	}		
}
